package com.tlx.rename;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.io.FilenameUtils;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

import com.tlx.core.util.TFileUtils;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;

@SpringBootApplication
@ComponentScan(basePackages = { "com.tlx" })
public class RenameNumberUtils {

    public static void main(String[] args) {
        SpringApplication.run(RenameNumberUtils.class, args);
         System.setProperty("realRename", "true");

        renameFileToDirByDir("E:\\TDownload\\361");
    }

    // 将文件名中的中文数字转换成阿拉伯数字
    public static void toNumber(String filePath) {
        vilidateFile(filePath);

        String oldName = FilenameUtils.getName(filePath);
        String newName = oldName;
        // 从5000倒数替换
        for (int i = 5000; i > -1; i--) {
            String chineseNumber = Convert.numberToChinese(i, false);
            System.out.println(chineseNumber);
            if (oldName.contains(chineseNumber)) {
                newName = oldName.replace(chineseNumber, i + "");
                break;
            }
        }
        if (!StrUtil.equals(oldName, newName)) {
            TFileUtils.renameOnlyName(filePath, newName);
            System.out.println(oldName + "\t --> \t " + newName);
        }

    }

    public static void toNumberByDir(String dirPath) {
        vilidateDir(dirPath);
        List<String> fileList = TFileUtils.getFileList(dirPath);
        for (String string : fileList) {
            toNumber(string);
        }
    }

    public static void renameFileToDir(String filePath) {
        vilidateFile(filePath);
        String newName = new File(filePath).getParentFile().getName();
        String ext = FilenameUtils.getExtension(filePath);
        newName = newName + "." + ext;
        TFileUtils.renameOnlyName(filePath, newName);
    }

    public static void renameFileToDirByDir(String dirPath, String... extArray) {
        vilidateDir(dirPath);

        // 处理当前目录
        List<String> fileList = new ArrayList<String>();
        if (extArray == null || extArray.length == 0) {
            fileList = TFileUtils.getFileList(dirPath);
        } else {
            fileList = TFileUtils.getFileListByExt(dirPath, extArray);
        }

        if (CollectionUtils.isEmpty(fileList)) {
            // 空跳过
        } else if (fileList.size() > 1) {
            throw new RuntimeException("重命名必须只有一个文件：" + dirPath);
        } else if (fileList.size() == 1) {
            renameFileToDir(fileList.get(0));
        }

        // 处理递归
        List<String> dirList = TFileUtils.getDirList(dirPath);
        for (String sonPath : dirList) {
            renameFileToDirByDir(sonPath, extArray);
        }
    }

    private static void vilidateFile(String filePath) {
        if (!new File(filePath).isFile()) {
            throw new RuntimeException("非文件不能重命名：" + filePath);
        }
    }

    private static void vilidateDir(String dirPath) {
        if (!new File(dirPath).isDirectory()) {
            throw new RuntimeException("非目录不能重命名：" + dirPath);
        }
    }

}
